package com.qdu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qdu.entity.Complete;
import com.qdu.entity.User;
import com.qdu.mapper.CompleteMapper;
import com.qdu.service.ICompleteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ellery2018
 * @since 2022-03-09
 */
@Service
@RequiredArgsConstructor
public class CompleteServiceImpl extends ServiceImpl<CompleteMapper, Complete> implements ICompleteService {
    private final CompleteMapper completeMapper;

    @Override
    public boolean endsign(Integer userid, Integer routeid) {
        Complete complete=completeMapper.selectOne(new QueryWrapper<Complete>()
                .eq("userId", userid)
                .eq("routeId", routeid));
        if(complete.getBeginState()==false) return false;
        complete.setRouteId(routeid);
        complete.setUserId(userid);
        Date mydate = new Date();
        complete.setEndDate(mydate);
        complete.setState("2");//线下已完成
        complete.setEndState(true);//终点已签到
        completeMapper.updateById(complete);
        return  true;
    }

    @Override
    public void onlinesign(Integer routeid, Integer userid) {
        System.out.println(routeid+"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        Complete complete = new Complete();
        complete.setRouteId(routeid);
        complete.setUserId(userid);
        Date mydate = new Date();
        complete.setBeginDate(mydate);
        complete.setEndDate(mydate);
        complete.setState("3");//线上已完成
        System.out.println(userid+"!!!!!!!!!!!!!!!!!!!!");
        completeMapper.insert(complete);
    }


    @Override
    public void startsign(Integer userid, Integer routeid) {


        Complete complete=completeMapper.selectOne(new QueryWrapper<Complete>()
                .eq("userId", userid)
                .eq("routeId", routeid));

        //Complete complete=completeMapper.selectById(userid);

        System.out.println("$#$$$$$$$$$$$$$$$$$$$$$$#"+complete+"%^^^^^^^");
        System.out.println("$#$$$$$$$$$$$$$$$$$$$$$$#"+userid+"%^^^^^^^"+routeid);
        if(complete==null){
            complete = new Complete();
            complete.setRouteId(routeid);
            complete.setUserId(userid);
            Date mydate = new Date();
            complete.setBeginDate(mydate);
            complete.setState("1");//线下进行中
            complete.setBeginState(true);//起点已签到
            completeMapper.insert(complete);
        }
        else{
            complete.setRouteId(routeid);
            complete.setUserId(userid);
            Date mydate = new Date();
            complete.setBeginDate(mydate);
            complete.setState("1");//线下进行中
            complete.setBeginState(true);//起点已签到
            completeMapper.updateById(complete);
        }
    }

    @Override
    public void refresh(Integer userid) {
        List<Complete> completelist=completeMapper.selectList(new QueryWrapper<Complete>()
                .eq("userId", userid)
                .eq("beginState", true).eq("endState",false));
        for(Complete complete:completelist){
            deletecomplete(complete.getId());
        }
    }

    @Override
    public List<Complete> findListByUId(Integer userid) {
        return completeMapper.selectList(new QueryWrapper<Complete>()
                .eq("userId", userid));
    }

    @Override
    public void deletecomplete(Integer completeid) {
        completeMapper.deleteById(completeid);
    }



    public double EARTH_RADIUS =6378.137;

    /**

     * 角度弧度计算公式rad()

     * 360度=2π π=Math.PI

     * x度 = x*π/360 弧度

     *

     * @param d

     * @return

     */

    public double rad(double d) {

        return d * Math.PI /180.0;

    }

    @Override
    public Integer findstate(Integer userid, Integer routeid) {
        Complete complete=completeMapper.selectOne(new QueryWrapper<Complete>()
                .eq("userId", userid)
                .eq("routeId", routeid));

        if(complete==null){
            return 0;
        }
        else{
            return  Integer.valueOf(complete.getState());
        }
    }

    /**

     * 根据两点间经纬度坐标（double值），计算两点间距离，单位为米

     *

     * @param lng1 经度1

     * @param lat1 纬度1

     * @param lng2 经度2

     * @param lat2 纬度2

     * @return

     */

    public double getDistance(double lng1, double lat1, double lng2, double lat2) {

        double radLat1 =rad(lat1);

        double radLat2 =rad(lat2);

        double a = radLat1 - radLat2;

        double b =rad(lng1) -rad(lng2);

        double s =2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a /2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b /2), 2)));

        s = s *EARTH_RADIUS;

        s = Math.round(s *10000d) /10000d;

        s = s *1000;

        return s;

    }

    private final static double EARTH_RADIUS2 = 6378137.0;
    /**
     * 计算两点距离
     *
     * @param lat_a
     * @param lng_a
     *
     * @param lat_b
     * @param lng_b
     * @return
     */
    public double getDistance2(double lat_a, double lng_a, double lat_b, double lng_b) {
        double radLat1 = (lat_a * Math.PI / 180.0);
        double radLat2 = (lat_b * Math.PI / 180.0);
        double a = radLat1 - radLat2;
        double b = (lng_a - lng_b) * Math.PI / 180.0;

        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
                + Math.cos(radLat1) * Math.cos(radLat2)
                * Math.pow(Math.sin(b / 2), 2)));

        s = s * EARTH_RADIUS2;
        s = Math.round(s * 10000d) / 10000d;
        return s;
    }

    @Override
    public boolean photosign(Integer userid, Integer routeid) {
        System.out.println("@@@@@@@@@@@@@@@@@@@"+userid+"$$$$$$$$$"+routeid);
        Complete complete=completeMapper.selectOne(new QueryWrapper<Complete>()
                .eq("userId", userid)
                .eq("routeId", routeid));
        Date mydate = new Date();
        complete.setPhotoState("1");//拍照审核中
        completeMapper.updateById(complete);
        return  true;
    }

    @Override
    public String findphotosign(Integer userid, Integer routeid) {
        Complete complete=completeMapper.selectOne(new QueryWrapper<Complete>()
                .eq("userId", userid)
                .eq("routeId", routeid));

        return  complete.getPhotoState();
    }

}
